<?php
namespace src\logic;
use \classlib\core as C;
class luser {
	const VISITOR   = 'visitor_%d';
	/**
	 * 获取某个用户订阅了哪些直播间
	 * @param int $userid
	 * @param int $sid
	 * @return array map 
	 */
	public static function myfeed($userid, $sid = 0) {
		$where = array('userid'=>$userid, array('airtime', C\config::$nowtime, \classlib\orm::OP_GT));
		if ($sid > 0) {
			$where['studioid'] = $sid;
		}
		$field = 'studioid as `key`, 1 as val';
		$maps  = C\model::load('main.log.subscibe')->get_mapper(0, -1, $where, $field);
		return $maps;
	}
	
	//获取指定用户关注的所有主播id
	public static function myfocus($userid, $isfocus=1) {
		$where = array('userid'=>$userid, 'focus'=>$isfocus);
		$field = 'fansid as `key`, 1 as val';
		$list  = C\model::load('main.pl.myfans')->set_dev($userid)->get_column(0, -1, $where, $field);
		return $list;
	}
	
	//用户是否关注了某个直播
	public static function isfocus($userid, $fansid) {
		$fansids = self::myfocus($userid);
		return in_array($fansid, $fansids)? 1:0;
	}
	
	//我关注的用户数量
	public static function attachs_ttl($userid, $isfocus=1) {
		$where = array('userid'=>$userid, 'focus'=>$isfocus);
		$ttl   = C\model::load('main.pl.myfans')->set_dev($userid)->get_total($where, 'count(1)');
		return $ttl;
	}
	
	//用户关注主播 或者取消关注
	public static function sfocus($userid, $fansid, $status) {
		$orm = C\model::load('main.pl.myfans');
		$orm->set_dev($fansid)->set_dev($userid);
		$studio = C\model::load('main.pl.studio')->get_item(array('userid'=>$fansid, 'status'=>1));
		if ($studio) {
			$sql = sprintf('call setFollowed(%d, %d, %d);', $userid, $fansid, $status);
			$orm->get_orm()->get_parser()->execute($sql);
		}
	}
	
	//获取一个用户的粉丝数目
	public static function gfans($userid) {
		$where = array('userid'=>$userid, 'focus'=>0);
		$xfans = C\model::load('main.pl.myfans')->set_dev($userid)->get_total($where);
		return intval($xfans);
	}
	
	//获取一个用户的关注的数目
	public static function gattens($userid) {
		$where = array('userid'=>$userid, 'focus'=>1);
		$xfans = C\model::load('main.pl.myfans')->set_dev($userid)->get_total($where);
		return intval($xfans);
	}
	
	//获取会员财务数据信息
	public static function byid($userid) {
		$user  = array('islogin'=>true, 'sign'=>'', 'isfocus'=>false);
		$user['user'] = C\model::load('main.pl.userinfo')->set_dev($userid)->get_one($userid);
		$user['money']= C\model::load('main.pl.finance')->set_dev($userid)->get_one($userid);
		return $user;
	}
	
	//更新会员财务数据信息
	public static function money($money, $bean, $userid) {
		$orm = C\model::load('main.pl.finance')->set_dev($userid);
		$sql = 'update '.$orm->get_table().' set lastcoin=`lastcoin`-'.$money.', conscoin=`conscoin`+'.$money.
		', lastbean=`lastbean`-'.$bean.', consbean=`consbean`+'.$bean.', stime='.time().
		' where userid='.$userid.' AND `lastcoin`-'.$money.' >= 0';
		$parse = $orm->get_orm()->get_parser();
		$parse->execute($sql);
		$affect_rows = $parse->get_affected_rows();
		if ($affect_rows) {
			$orm->idclear($userid);
		}
		return $affect_rows;
	}
	//获取会员的最近访客资料信息
	public static function gvisitor($userid, $limit = 30) {
		$ckey  = sprintf(self::VISITOR, $userid);
		$list  = C\model::$cacher->get($ckey);
		if (empty($list)) {
			$list = C\model::load('main.log.visitor')->set_dev($userid)->get_orm()
				->where('userid', $userid)->orderby('stime', \classlib\orm::DESC)
				->get_list(NULL, 0, $limit);
			if ($list) {
				C\model::$cacher->set($ckey, $list);
			}
		}
		return $list;
	}
	
	//新增会员访问新的信息记录
	public static function svisitor($userid, $visitor) {
		$ckey  = sprintf(self::VISITOR, $userid);
		C\model::load('main.log.visitor')->set_dev($userid)
			->get_orm()->duplicate($visitor)->insert($visitor);
		C\model::$cacher->delete($ckey);
	}
}